using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Infragistics.UltraChart.Data.Series;
using Infragistics.UltraChart.Resources.Appearance;
using PluginFrame;

namespace PcrDataStructure
{
    [Serializable]
    public class PcrCell : IPcrCell
    {
        protected readonly int mChannelCount;

        protected readonly int mCellID;

        protected readonly string mCellPosition;

        protected readonly ISubModule mSubModule;

        protected List<byte[,]> mLightRawByteData;

        protected List<double>[] mLightRawData;

        protected List<double>[] mLightTemperature;

        protected Dictionary<string, string>[] mCellInformation;

        protected double[] mCycleValue;

        protected double[] mConcentration;

        protected bool[] mNeedReAnalysis;

        protected List<IPcrCell>[] mMultiCellList;

        protected int mSourceID;

        protected Color[] mChannelDetectionTypeColor;

        protected IPcrExperimentProject mProject;

        protected List<IPcrCell> mMultiTubeCells;

        protected bool mIsBad;

        [NonSerialized]
        protected List<double> mHRMRawTemperatureList;

        [NonSerialized]
        protected List<double> mHRMRawFluorList;

        [NonSerialized]
        protected List<double>[] mTempLightRawData;

        [NonSerialized]
        protected bool mIsSaveTempLightRawData;

        [NonSerialized]
        protected List<double>[] mNormalLightData;

        [NonSerialized]
        protected List<double>[] mCurveLightData;

        [NonSerialized]
        protected double[] mBaseLineAvgValue;

        [NonSerialized]
        protected double[] mNormalBaseLineAvgValue;

        [NonSerialized]
        protected int[] mAutoBaseLineStartValue;

        [NonSerialized]
        protected int[] mAutoBaseLineStopValue;

        [NonSerialized]
        protected int[] mAutoBaseLineRotationCount;

        [NonSerialized]
        protected XYSeries[] mCellRawCurveSeries;

        [NonSerialized]
        protected XYSeries[] mCellAmplificationCurveSeries;

        [NonSerialized]
        protected XYSeries[] mCellMeltingCurveSeries;

        [NonSerialized]
        protected XYSeries[] mCellMeltingDevCurveSeries;

        [NonSerialized]
        protected XYSeries mHRMNormalizedCurveSeries;

        [NonSerialized]
        protected XYSeries mHRMNormalizedShiftCurveSeries;

        [NonSerialized]
        protected XYSeries mHRMPeakCurveSeries;

        [NonSerialized]
        protected XYSeries mHRMDiffCurveSeries;

        [NonSerialized]
        protected double[] mAmplificationCurveMin;

        [NonSerialized]
        protected double[] mAmplificationCurveMax;

        [NonSerialized]
        protected double[] mRawCurveMin;

        [NonSerialized]
        protected double[] mRawCurveMax;

        [NonSerialized]
        protected double[] mMeltingCurveMin;

        [NonSerialized]
        protected double[] mMeltingCurveMax;

        [NonSerialized]
        protected double[] mMeltingDevCurveMin;

        [NonSerialized]
        protected double[] mMeltingDevCurveMax;

        [NonSerialized]
        protected double mHRMNormalizedCurveMin;

        [NonSerialized]
        protected double mHRMNormalizedCurveMax;

        [NonSerialized]
        protected double mHRMNormalizedShiftCurveMin;

        [NonSerialized]
        protected double mHRMNormalizedShiftCurveMax;

        [NonSerialized]
        protected double mHRMPeakCurveMin;

        [NonSerialized]
        protected double mHRMPeakCurveMax;

        [NonSerialized]
        protected double mHRMDiffCurveMin;

        [NonSerialized]
        protected double mHRMDiffCurveMax;

        [NonSerialized]
        protected int mDataRowIndex;

        [NonSerialized]
        protected List<double>[] mInterpResult;

        [NonSerialized]
        protected bool mIsSetInterpResult;

        [NonSerialized]
        protected int[] mMeltingResultDataCount;

        [NonSerialized]
        protected int[] mRuleCalculationValueCount;

        [NonSerialized]
        protected XYSeries[] mAlleleSeries;

        [NonSerialized]
        protected int mHRMGroupIndex;

        public List<byte[,]> LightRawByteData
        {
            get
            {
                return mLightRawByteData;
            }
            set
            {
                mLightRawByteData = value;
            }
        }

        public double HRMDiffCurveMin
        {
            get
            {
                return mHRMDiffCurveMin;
            }
            set
            {
                mHRMDiffCurveMin = value;
            }
        }

        public double HRMDiffCurveMax
        {
            get
            {
                return mHRMDiffCurveMax;
            }
            set
            {
                mHRMDiffCurveMax = value;
            }
        }

        public int HRMGroupIndex
        {
            get
            {
                return mHRMGroupIndex;
            }
            set
            {
                mHRMGroupIndex = value;
            }
        }

        public List<double> HRMRawTemperatureList
        {
            get
            {
                return mHRMRawTemperatureList;
            }
            set
            {
                mHRMRawTemperatureList = value;
            }
        }

        public List<double> HRMRawFluorList
        {
            get
            {
                return mHRMRawFluorList;
            }
            set
            {
                mHRMRawFluorList = value;
            }
        }

        public List<IPcrCell> MultiTubeCells
        {
            get
            {
                if (mMultiTubeCells == null || mMultiTubeCells.Count == 0)
                {
                    mMultiTubeCells = new List<IPcrCell>();
                    mMultiTubeCells.Add(this);
                }
                return mMultiTubeCells;
            }
            set
            {
                mMultiTubeCells = value;
            }
        }

        public IPcrExperiment PcrExperiment
        {
            get
            {
                return mSubModule.PcrExperiment;
            }
        }

        public double MeltingCurveMin
        {
            get
            {
                double num = mMeltingCurveMin[0];
                for (int i = 1; i < mChannelCount; i++)
                {
                    if (mMeltingCurveMin[i] < num)
                    {
                        num = mMeltingCurveMin[i];
                    }
                }
                return num;
            }
            set
            {
                mMeltingCurveMin[0] = value;
            }
        }

        public double MeltingCurveMax
        {
            get
            {
                double num = mMeltingCurveMax[0];
                for (int i = 1; i < mChannelCount; i++)
                {
                    if (mMeltingCurveMax[i] > num)
                    {
                        num = mMeltingCurveMax[i];
                    }
                }
                return num;
            }
            set
            {
                mMeltingCurveMax[0] = value;
            }
        }

        public double MeltingDevCurveMin
        {
            get
            {
                double num = mMeltingDevCurveMin[0];
                for (int i = 1; i < mChannelCount; i++)
                {
                    if (mMeltingDevCurveMin[i] < num)
                    {
                        num = mMeltingDevCurveMin[i];
                    }
                }
                return num;
            }
            set
            {
                mMeltingDevCurveMin[0] = value;
            }
        }

        public double MeltingDevCurveMax
        {
            get
            {
                double num = mMeltingDevCurveMax[0];
                for (int i = 1; i < mChannelCount; i++)
                {
                    if (mMeltingDevCurveMax[i] > num)
                    {
                        num = mMeltingDevCurveMax[i];
                    }
                }
                return num;
            }
            set
            {
                mMeltingDevCurveMax[0] = value;
            }
        }

        public double AmplificationCurveMin
        {
            get
            {
                double num = mAmplificationCurveMin[0];
                for (int i = 1; i < mChannelCount; i++)
                {
                    if (mAmplificationCurveMin[i] < num)
                    {
                        num = mAmplificationCurveMin[i];
                    }
                }
                return num;
            }
            set
            {
                mAmplificationCurveMin[0] = value;
            }
        }

        public double AmplificationCurveMax
        {
            get
            {
                double num = mAmplificationCurveMax[0];
                for (int i = 1; i < mChannelCount; i++)
                {
                    if (mAmplificationCurveMax[i] > num)
                    {
                        num = mAmplificationCurveMax[i];
                    }
                }
                return num;
            }
            set
            {
                mAmplificationCurveMax[0] = value;
            }
        }

        public double RawCurveMin
        {
            get
            {
                double num = mRawCurveMin[0];
                for (int i = 1; i < mChannelCount; i++)
                {
                    if (mRawCurveMin[i] < num)
                    {
                        num = mRawCurveMin[i];
                    }
                }
                return num;
            }
        }

        public double RawCurveMax
        {
            get
            {
                double num = mRawCurveMax[0];
                for (int i = 1; i < mChannelCount; i++)
                {
                    if (mRawCurveMax[i] > num)
                    {
                        num = mRawCurveMax[i];
                    }
                }
                return num;
            }
        }

        public int AmplificationStartCycle
        {
            get
            {
                return PcrExperiment.AmplificationStartCycle;
            }
        }

        public int AmplificationStopCycle
        {
            get
            {
                return PcrExperiment.AmplificationStopCycle;
            }
        }

        public int MeltingStartCycle
        {
            get
            {
                return PcrExperiment.MeltingStartCycle;
            }
        }

        public int MeltingStopCycle
        {
            get
            {
                return PcrExperiment.MeltingStopCycle;
            }
        }

        public int SourceID
        {
            get
            {
                return mSourceID;
            }
            set
            {
                mSourceID = value;
            }
        }

        int IPcrCell.CellID
        {
            get
            {
                return mCellID;
            }
        }

        ISubModule IPcrCell.SubModule
        {
            get
            {
                return mSubModule;
            }
        }

        string IPcrCell.CellPosition
        {
            get
            {
                return mCellPosition;
            }
        }

        public IPcrExperimentProject Project
        {
            get
            {
                return mProject;
            }
            set
            {
                mProject = value;
            }
        }

        public bool IsSetInterpResult
        {
            get
            {
                return mIsSetInterpResult;
            }
            set
            {
                mIsSetInterpResult = value;
            }
        }

        public List<double>[] InterpResult
        {
            get
            {
                return mInterpResult;
            }
            set
            {
                mInterpResult = value;
            }
        }

        public int DataRowIndex
        {
            get
            {
                return mDataRowIndex;
            }
            set
            {
                mDataRowIndex = value;
            }
        }

        public bool IsBad
        {
            get
            {
                return mIsBad;
            }
            set
            {
                mIsBad = value;
            }
        }

        public XYSeries[] AlleleSeries
        {
            get
            {
                return mAlleleSeries;
            }
            set
            {
                mAlleleSeries = value;
            }
        }

        public double HRMNormalizedCurveMin
        {
            get
            {
                return mHRMNormalizedCurveMin;
            }
            set
            {
                mHRMNormalizedCurveMin = value;
            }
        }

        public double HRMNormalizedCurveMax
        {
            get
            {
                return mHRMNormalizedCurveMax;
            }
            set
            {
                mHRMNormalizedCurveMax = value;
            }
        }

        public double HRMNormalizedShiftCurveMin
        {
            get
            {
                return mHRMNormalizedShiftCurveMin;
            }
            set
            {
                mHRMNormalizedShiftCurveMin = value;
            }
        }

        public double HRMNormalizedShiftCurveMax
        {
            get
            {
                return mHRMNormalizedShiftCurveMax;
            }
            set
            {
                mHRMNormalizedShiftCurveMax = value;
            }
        }

        public double HRMPeakCurveMin
        {
            get
            {
                return mHRMPeakCurveMin;
            }
            set
            {
                mHRMPeakCurveMin = value;
            }
        }

        public double HRMPeakCurveMax
        {
            get
            {
                return mHRMPeakCurveMax;
            }
            set
            {
                mHRMPeakCurveMax = value;
            }
        }

        public PcrCell(int cellID, string cellPosition, ISubModule subModule, int channelCount, List<string> columnList)
        {
            mCellID = cellID;
            mCellPosition = cellPosition;
            mSubModule = subModule;
            mChannelCount = channelCount;
            mCellInformation = new Dictionary<string, string>[channelCount];
            mNeedReAnalysis = new bool[channelCount];
            mChannelDetectionTypeColor = new Color[channelCount];
            mMultiTubeCells = new List<IPcrCell>();
            mMultiTubeCells.Add(this);
            for (int i = 0; i < channelCount; i++)
            {
                mNeedReAnalysis[i] = true;
                mCellInformation[i] = new Dictionary<string, string>();
                for (int j = 0; j < columnList.Count; j++)
                {
                    mCellInformation[i].Add(columnList[j], string.Empty);
                }
                mCellInformation[i]["CellIDKey"] = mCellID.ToString();
                mCellInformation[i]["CellPositionKey"] = mCellPosition;
                mCellInformation[i]["ChannelKey"] = (i + 1).ToString();
                mChannelDetectionTypeColor[i] = Color.Black;
            }
            InitializeRuleCalculationValueCount();
            InitializeMeltingResultDataCount();
        }

        private void SetCurveDisplay(XYSeries curveSeries, bool isDisplay, Color curveColor)
        {
            if (isDisplay && curveColor != Color.Empty)
            {
                curveSeries.PEs.Clear();
                curveSeries.PEs.Add(new PaintElement(curveColor));
            }
            curveSeries.Visible = isDisplay;
        }

        public void InitializeRawCurveSeries(int channelIndex)
        {
            if (mCellRawCurveSeries == null)
            {
                mCellRawCurveSeries = new XYSeries[mChannelCount];
            }
            if (mRawCurveMin == null)
            {
                mRawCurveMin = new double[mChannelCount];
                for (int i = 0; i < mChannelCount; i++)
                {
                    mRawCurveMin[i] = double.MaxValue;
                }
            }
            if (mRawCurveMax == null)
            {
                mRawCurveMax = new double[mChannelCount];
                for (int j = 0; j < mChannelCount; j++)
                {
                    mRawCurveMax[j] = double.MinValue;
                }
            }
            mRawCurveMin[channelIndex] = double.MaxValue;
            mRawCurveMax[channelIndex] = double.MinValue;
            if (mCellRawCurveSeries[channelIndex] == null)
            {
                mCellRawCurveSeries[channelIndex] = new XYSeries();
            }
            mCellRawCurveSeries[channelIndex].Key = mCellID.ToString();
            mCellRawCurveSeries[channelIndex].Label = channelIndex.ToString();
            mCellRawCurveSeries[channelIndex].Points.Clear();
            mCellRawCurveSeries[channelIndex].Points.Add(new XYDataPoint(-1.0, -1.0, "", true));
            mCellRawCurveSeries[channelIndex].Visible = false;
        }

        public void InitializeMeltingCurveSeries(int channelIndex)
        {
            if (mCellMeltingCurveSeries == null)
            {
                mCellMeltingCurveSeries = new XYSeries[mChannelCount];
            }
            if (mCellMeltingDevCurveSeries == null)
            {
                mCellMeltingDevCurveSeries = new XYSeries[mChannelCount];
            }
            if (mMeltingCurveMin == null || mMeltingDevCurveMin == null)
            {
                mMeltingCurveMin = new double[mChannelCount];
                for (int i = 0; i < mChannelCount; i++)
                {
                    mMeltingCurveMin[i] = double.MaxValue;
                }
                mMeltingDevCurveMin = new double[mChannelCount];
                for (int j = 0; j < mChannelCount; j++)
                {
                    mMeltingDevCurveMin[j] = double.MaxValue;
                }
            }
            if (mMeltingCurveMax == null || mMeltingDevCurveMax == null)
            {
                mMeltingCurveMax = new double[mChannelCount];
                for (int k = 0; k < mChannelCount; k++)
                {
                    mMeltingCurveMax[k] = double.MinValue;
                }
                mMeltingDevCurveMax = new double[mChannelCount];
                for (int l = 0; l < mChannelCount; l++)
                {
                    mMeltingDevCurveMax[l] = double.MinValue;
                }
            }
            mMeltingCurveMin[channelIndex] = double.MaxValue;
            mMeltingCurveMax[channelIndex] = double.MinValue;
            if (mMeltingDevCurveMin != null)
            {
                mMeltingDevCurveMin[channelIndex] = double.MaxValue;
                mMeltingDevCurveMax[channelIndex] = double.MinValue;
            }
            if (mCellMeltingCurveSeries[channelIndex] == null)
            {
                mCellMeltingCurveSeries[channelIndex] = new XYSeries();
            }
            if (mCellMeltingDevCurveSeries != null && mCellMeltingDevCurveSeries[channelIndex] == null)
            {
                mCellMeltingDevCurveSeries[channelIndex] = new XYSeries();
            }
            mCellMeltingCurveSeries[channelIndex].Key = mCellID.ToString();
            mCellMeltingCurveSeries[channelIndex].Label = channelIndex.ToString();
            mCellMeltingCurveSeries[channelIndex].Points.Clear();
            mCellMeltingCurveSeries[channelIndex].Points.Add(new XYDataPoint(-1.0, -1.0, "", true));
            mCellMeltingCurveSeries[channelIndex].Visible = false;
            mCellMeltingDevCurveSeries[channelIndex].Key = mCellID.ToString();
            mCellMeltingDevCurveSeries[channelIndex].Label = channelIndex.ToString();
            mCellMeltingDevCurveSeries[channelIndex].Points.Clear();
            mCellMeltingDevCurveSeries[channelIndex].Points.Add(new XYDataPoint(-1.0, -1.0, "", true));
            mCellMeltingDevCurveSeries[channelIndex].Visible = false;
        }

        public void InitializeHRMCurveSeries(int channelIndex)
        {
            if (mCellMeltingCurveSeries == null)
            {
                mCellMeltingCurveSeries = new XYSeries[mChannelCount];
            }
            if (mMeltingCurveMin == null)
            {
                mMeltingCurveMin = new double[mChannelCount];
                for (int i = 0; i < mChannelCount; i++)
                {
                    mMeltingCurveMin[i] = double.MaxValue;
                }
            }
            if (mMeltingCurveMax == null)
            {
                mMeltingCurveMax = new double[mChannelCount];
                for (int j = 0; j < mChannelCount; j++)
                {
                    mMeltingCurveMax[j] = double.MinValue;
                }
            }
            mMeltingCurveMin[channelIndex] = double.MaxValue;
            mMeltingCurveMax[channelIndex] = double.MinValue;
            if (mCellMeltingCurveSeries[channelIndex] == null)
            {
                mCellMeltingCurveSeries[channelIndex] = new XYSeries();
            }
            mCellMeltingCurveSeries[channelIndex].Key = mCellID.ToString();
            mCellMeltingCurveSeries[channelIndex].Label = channelIndex.ToString();
            mCellMeltingCurveSeries[channelIndex].Points.Clear();
            mCellMeltingCurveSeries[channelIndex].Visible = false;
            if (mHRMNormalizedCurveSeries == null)
            {
                mHRMNormalizedCurveSeries = new XYSeries();
                mHRMNormalizedCurveSeries.Key = mCellID.ToString();
                mHRMNormalizedCurveSeries.Label = channelIndex.ToString();
                mHRMNormalizedCurveSeries.Points.Clear();
                mHRMNormalizedCurveSeries.Visible = false;
                mHRMNormalizedShiftCurveSeries = new XYSeries();
                mHRMNormalizedShiftCurveSeries.Key = mCellID.ToString();
                mHRMNormalizedShiftCurveSeries.Label = channelIndex.ToString();
                mHRMNormalizedShiftCurveSeries.Points.Clear();
                mHRMNormalizedShiftCurveSeries.Visible = false;
                mHRMPeakCurveSeries = new XYSeries();
                mHRMPeakCurveSeries.Key = mCellID.ToString();
                mHRMPeakCurveSeries.Label = channelIndex.ToString();
                mHRMPeakCurveSeries.Points.Clear();
                mHRMPeakCurveSeries.Visible = false;
                mHRMDiffCurveSeries = new XYSeries();
                mHRMDiffCurveSeries.Key = mCellID.ToString();
                mHRMDiffCurveSeries.Label = channelIndex.ToString();
                mHRMDiffCurveSeries.Points.Clear();
                mHRMDiffCurveSeries.Visible = false;
            }
        }

        public void InitializeMeltingResultDataCount()
        {
            mMeltingResultDataCount = new int[mChannelCount];
        }

        public void InitializeRuleCalculationValueCount()
        {
            mRuleCalculationValueCount = new int[mChannelCount];
        }

        public void ClearRawCurveSeries()
        {
            for (int i = 0; i < mChannelCount; i++)
            {
                if (mCellRawCurveSeries == null || mCellRawCurveSeries[i] == null)
                {
                    InitializeRawCurveSeries(i);
                }
                mCellRawCurveSeries[i].Points.Clear();
                mCellRawCurveSeries[i].Points.Add(new XYDataPoint(-1.0, -1.0, "", true));
            }
        }

        public void ClearMeltingCurveSeries()
        {
            for (int i = 0; i < mChannelCount; i++)
            {
                if (mCellMeltingCurveSeries == null || mCellMeltingCurveSeries[i] == null || mCellMeltingDevCurveSeries == null || mCellMeltingDevCurveSeries[i] == null)
                {
                    InitializeMeltingCurveSeries(i);
                }
                mCellMeltingCurveSeries[i].Points.Clear();
                mCellMeltingCurveSeries[i].Points.Add(new XYDataPoint(-1.0, -1.0, "", true));
                mCellMeltingDevCurveSeries[i].Points.Clear();
                mCellMeltingDevCurveSeries[i].Points.Add(new XYDataPoint(-1.0, -1.0, "", true));
            }
        }

        public void ClearRealTimeAmplicationCurveSeries()
        {
            for (int i = 0; i < mChannelCount; i++)
            {
                if (mCellAmplificationCurveSeries == null || mCellAmplificationCurveSeries[i] == null)
                {
                    InitializeAmplificationCurveSeries(i);
                }
                SetBaseLineAvgValue(i, 0.0);
                mCellAmplificationCurveSeries[i].Points.Clear();
                mCellAmplificationCurveSeries[i].Points.Add(new XYDataPoint(-1.0, -1.0, "", true));
            }
        }

        public void InitializeAmplificationCurveSeries(int channelIndex)
        {
            if (mCellAmplificationCurveSeries == null)
            {
                mCellAmplificationCurveSeries = new XYSeries[mChannelCount];
            }
            if (mAmplificationCurveMin == null)
            {
                mAmplificationCurveMin = new double[mChannelCount];
                for (int i = 0; i < mChannelCount; i++)
                {
                    mAmplificationCurveMin[i] = double.MaxValue;
                }
            }
            if (mAmplificationCurveMax == null)
            {
                mAmplificationCurveMax = new double[mChannelCount];
                for (int j = 0; j < mChannelCount; j++)
                {
                    mAmplificationCurveMax[j] = double.MinValue;
                }
            }
            mAmplificationCurveMin[channelIndex] = double.MaxValue;
            mAmplificationCurveMax[channelIndex] = double.MinValue;
            if (mCellAmplificationCurveSeries[channelIndex] == null)
            {
                mCellAmplificationCurveSeries[channelIndex] = new XYSeries();
            }
            mCellAmplificationCurveSeries[channelIndex].Points.Clear();
            mCellAmplificationCurveSeries[channelIndex].Key = mCellID.ToString();
            mCellAmplificationCurveSeries[channelIndex].Label = channelIndex.ToString();
        }

        public void InitializeAllelePointSeries()
        {
            mAlleleSeries = new XYSeries[3];
            for (int i = 0; i < mAlleleSeries.Length; i++)
            {
                mAlleleSeries[i] = new XYSeries();
            }
        }

        public void InitializeLightDataList(int detectionCycleCount, int dataByteCount)
        {
            if (detectionCycleCount <= 0)
            {
                return;
            }
            mLightRawByteData = new List<byte[,]>(detectionCycleCount);
            mLightRawData = new List<double>[mChannelCount];
            mLightTemperature = new List<double>[mChannelCount];
            for (int i = 0; i < mChannelCount; i++)
            {
                mLightRawData[i] = new List<double>(detectionCycleCount);
                for (int j = 0; j < detectionCycleCount; j++)
                {
                    mLightRawData[i].Add(double.NaN);
                }
            }
            for (int k = 0; k < detectionCycleCount; k++)
            {
                mLightRawByteData.Add(IntializeNewDataArray(dataByteCount));
            }
        }

        public void AddLightDataList(int newCycleCount, int dataByteCount)
        {
            if (newCycleCount <= 0)
            {
                return;
            }
            for (int i = 0; i < mChannelCount; i++)
            {
                for (int j = 0; j < newCycleCount; j++)
                {
                    mLightRawData[i].Add(double.NaN);
                    mNormalLightData[i].Add(double.NaN);
                }
            }
            for (int k = 0; k < newCycleCount; k++)
            {
                mLightRawByteData.Add(IntializeNewDataArray(dataByteCount));
            }
        }

        public void InitializeAnalysisDataList()
        {
            if (mLightRawData == null)
            {
                return;
            }
            mNormalLightData = new List<double>[mLightRawData.Length];
            mCurveLightData = new List<double>[mLightRawData.Length];
            mBaseLineAvgValue = new double[mLightRawData.Length];
            mNormalBaseLineAvgValue = new double[mLightRawData.Length];
            mAutoBaseLineStartValue = new int[mLightRawData.Length];
            mAutoBaseLineStopValue = new int[mLightRawData.Length];
            mAutoBaseLineRotationCount = new int[mLightRawData.Length];
            mCycleValue = new double[mLightRawData.Length];
            mConcentration = new double[mLightRawData.Length];
            mMultiCellList = new List<IPcrCell>[mLightRawData.Length];
            for (int i = 0; i < mNormalLightData.Length; i++)
            {
                mNormalLightData[i] = new List<double>();
                mCurveLightData[i] = new List<double>();
                mMultiCellList[i] = new List<IPcrCell>();
                for (int j = 0; j < mLightRawData[i].Count; j++)
                {
                    mNormalLightData[i].Add(double.NaN);
                }
                Application.DoEvents();
            }
        }

        private byte[,] IntializeNewDataArray(int dataByteCount)
        {
            byte[,] array = new byte[mChannelCount, dataByteCount];
            for (int i = 0; i < mChannelCount; i++)
            {
                for (int j = 0; j < dataByteCount; j++)
                {
                    array[i, j] = byte.MaxValue;
                }
            }
            return array;
        }

        public void SetDetectionTypeColor(int channelIndex, Color color)
        {
            mChannelDetectionTypeColor[channelIndex] = color;
        }

        public Color GetDetectionTypeColor(int channelIndex)
        {
            return mChannelDetectionTypeColor[channelIndex];
        }

        public XYSeries GetRawCurveSeries(int channelIndex)
        {
            if (mCellRawCurveSeries == null || mCellRawCurveSeries[channelIndex] == null)
            {
                InitializeRawCurveSeries(channelIndex);
            }
            return mCellRawCurveSeries[channelIndex];
        }

        public XYSeries GetHRMNormalizedCurveSeries()
        {
            return mHRMNormalizedCurveSeries ?? (mHRMNormalizedCurveSeries = new XYSeries());
        }

        public XYSeries GetHRMNormalizedShiftCurveSeries()
        {
            return mHRMNormalizedShiftCurveSeries ?? (mHRMNormalizedShiftCurveSeries = new XYSeries());
        }

        public XYSeries GetHRMPeakCurveSeries()
        {
            return mHRMPeakCurveSeries ?? (mHRMPeakCurveSeries = new XYSeries());
        }

        public XYSeries GetHRMDiffCurveSeries()
        {
            return mHRMDiffCurveSeries ?? (mHRMDiffCurveSeries = new XYSeries());
        }

        public XYSeries GetMeltingCurveSeries(int channelIndex)
        {
            if (mCellMeltingCurveSeries == null || mCellMeltingCurveSeries[channelIndex] == null)
            {
                InitializeMeltingCurveSeries(channelIndex);
            }
            return mCellMeltingCurveSeries[channelIndex];
        }

        public XYSeries GetMeltingDevCurveSeries(int channelIndex)
        {
            if (mCellMeltingDevCurveSeries == null || mCellMeltingDevCurveSeries[channelIndex] == null)
            {
                InitializeMeltingCurveSeries(channelIndex);
            }
            return mCellMeltingDevCurveSeries[channelIndex];
        }

        public int GetAmplificationCurvePointCount(int channelIndex)
        {
            return GetAmplificationCurveSeries(channelIndex).Points.Count;
        }

        public XYSeries GetAmplificationCurveSeries(int channelIndex)
        {
            if (mCellAmplificationCurveSeries != null && mCellAmplificationCurveSeries[channelIndex] != null)
            {
                return mCellAmplificationCurveSeries[channelIndex];
            }
            return null;
        }

        public List<IPcrCell> GetMultiCellList(int channelIndex)
        {
            return mMultiCellList[channelIndex];
        }

        public double GetRawData(int channelIndex, int cycleValue)
        {
            int num = cycleValue - 1;
            if (channelIndex < mLightRawData.Length && num < mLightRawData[channelIndex].Count)
            {
                return mLightRawData[channelIndex][num];
            }
            return 0.0;
        }

        public int GetChannelRawDataPoint(int channelIndex)
        {
            try
            {
                if (mLightRawData == null)
                {
                    return 0;
                }
                if (channelIndex >= mLightRawData.Length)
                {
                    return 0;
                }
                int count = mLightRawData[channelIndex].Count;
                for (int num = count - 1; num > PcrExperiment.AmplificationStartCycle; num--)
                {
                    if (!double.IsNaN(mLightRawData[channelIndex][num]))
                    {
                        return num + 1;
                    }
                }
                return 0;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public double[] GetChannelRawData(int channelIndex, bool isOnlyAmplification)
        {
            if (channelIndex < mLightRawData.Length)
            {
                int num = PcrExperiment.RunningStatus.DetectCycle;
                if (isOnlyAmplification && num > PcrExperiment.AmplificationStopCycle)
                {
                    num = PcrExperiment.AmplificationStopCycle;
                }
                int num2 = num;
                while (num2 > PcrExperiment.AmplificationStartCycle && double.IsNaN(mLightRawData[channelIndex][num2 - 1]))
                {
                    num--;
                    num2--;
                }
                double[] array = new double[num];
                mLightRawData[channelIndex].CopyTo(0, array, 0, num);
                return array;
            }
            return null;
        }

        public void AddLightTemperature(int channelIndex, double temperature)
        {
            if (mLightTemperature == null)
            {
                mLightTemperature = new List<double>[mChannelCount];
                for (int i = 0; i < mChannelCount; i++)
                {
                    mLightTemperature[i] = new List<double>();
                }
            }
            if (mLightTemperature[channelIndex] == null)
            {
                mLightTemperature[channelIndex] = new List<double>();
            }
            mLightTemperature[channelIndex].Add(temperature);
        }

        public double GetLightTemperature(int channelIndex, int cycleValue)
        {
            int num = cycleValue - 1;
            if (channelIndex < mLightTemperature.Length && num < mLightTemperature[channelIndex].Count)
            {
                return mLightTemperature[channelIndex][num];
            }
            return 0.0;
        }

        public double GetChannelLastLightTemperature(int channelIndex)
        {
            if (channelIndex < mLightTemperature.Length)
            {
                int index = mLightTemperature[channelIndex].Count - 1;
                return mLightTemperature[channelIndex][index];
            }
            return 0.0;
        }

        public List<double> GetChannelLightTemperatureList(int channelIndex)
        {
            if (channelIndex < mLightTemperature.Length)
            {
                return mLightTemperature[channelIndex];
            }
            return null;
        }

        public double GetNormalLightData(int channelIndex, int cycleIndex)
        {
            return mNormalLightData[channelIndex][cycleIndex - 1];
        }

        public double[] GetAmplificationLightDataArray(int channelIndex)
        {
            int num = AmplificationStartCycle;
            int detectCycle = PcrExperiment.RunningStatus.DetectCycle;
            if (detectCycle == 0)
            {
                return new double[0];
            }
            if (num == 0)
            {
                num = MeltingStartCycle;
            }
            if (num > detectCycle)
            {
                return new double[0];
            }
            return GetAmplificationLightDataArray(channelIndex, num, detectCycle);
        }

        public double[] GetAmplificationLightDataArray(int channelIndex, int startCycle, int stopCycle)
        {
            int num = stopCycle;
            int num2 = num;
            while (num2 > PcrExperiment.AmplificationStartCycle && double.IsNaN(mNormalLightData[channelIndex][num2 - 1]))
            {
                num--;
                num2--;
            }
            double[] array = new double[num - startCycle + 1];
            mNormalLightData[channelIndex].CopyTo(0, array, startCycle - 1, array.Length);
            return array;
        }

        public double GetCurveLightData(int channelIndex, int dataIndex)
        {
            return mCellAmplificationCurveSeries[channelIndex].Points[dataIndex].ValueY;
        }

        public bool GetIsNeedAnalysis(int channelIndex)
        {
            return mNeedReAnalysis[channelIndex];
        }

        public double GetCycleValue(int channelIndex)
        {
            return mCycleValue[channelIndex];
        }

        public void SetMultiCellList(int channelIndex, List<IPcrCell> pcrCellList)
        {
            mMultiCellList[channelIndex] = pcrCellList;
        }

        private bool CheckProgramAmplificationDetectionSegment()
        {
            if (mProject == null)
            {
                return false;
            }
            if (mProject.PcrProgram == null)
            {
                return false;
            }
            if (mProject.PcrProgram.AmplificationDetectionCount <= 0)
            {
                return false;
            }
            return true;
        }

        private bool CheckProgramMeltingDetectionSegment()
        {
            if (mProject == null)
            {
                return false;
            }
            if (mProject.PcrProgram == null)
            {
                return false;
            }
            if (mProject.PcrProgram.MeltingDetectionCount <= 0)
            {
                return false;
            }
            return true;
        }

        public bool CheckChannelIsAvailable(int channelIndex)
        {
            if (mProject != null)
            {
                IProjectItem[] projectItems = mProject.ProjectItems;
                foreach (IProjectItem projectItem in projectItems)
                {
                    if (projectItem != null && projectItem.ChannelConfiguration.IsSelected && projectItem.ChannelConfiguration.Position == channelIndex)
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public void SetRawCurveDisplay(int channelIndex, bool isDisplay, Color curveColor)
        {
            try
            {
                if (!isDisplay || CheckProgramAmplificationDetectionSegment())
                {
                    if (mCellRawCurveSeries == null || mCellRawCurveSeries[channelIndex] == null)
                    {
                        InitializeRawCurveSeries(channelIndex);
                    }
                    SetCurveDisplay(mCellRawCurveSeries[channelIndex], isDisplay, curveColor);
                }
            }
            catch (Exception)
            {
            }
        }

        public void SetMeltingCurveDisplay(int channelIndex, bool isDisplay, Color curveColor)
        {
            try
            {
                if (!isDisplay || CheckProgramMeltingDetectionSegment())
                {
                    if (mCellMeltingCurveSeries == null || mCellMeltingCurveSeries[channelIndex] == null)
                    {
                        InitializeMeltingCurveSeries(channelIndex);
                    }
                    SetCurveDisplay(mCellMeltingCurveSeries[channelIndex], isDisplay, curveColor);
                }
            }
            catch (Exception)
            {
            }
        }

        public void SetHRMNormalizedShiftDisplay(int channelIndex, bool isDisplay, Color curveColor)
        {
            try
            {
                if (!isDisplay || CheckProgramMeltingDetectionSegment())
                {
                    if (mHRMNormalizedShiftCurveSeries == null)
                    {
                        InitializeHRMCurveSeries(channelIndex);
                    }
                    SetCurveDisplay(mHRMNormalizedShiftCurveSeries, isDisplay, curveColor);
                }
            }
            catch (Exception)
            {
            }
        }

        public void SetHRMNormalizedCurveDisplay(int channelIndex, bool isDisplay, Color curveColor)
        {
            try
            {
                if (!isDisplay || CheckProgramMeltingDetectionSegment())
                {
                    if (mHRMNormalizedCurveSeries == null)
                    {
                        InitializeHRMCurveSeries(channelIndex);
                    }
                    SetCurveDisplay(mHRMNormalizedCurveSeries, isDisplay, curveColor);
                }
            }
            catch (Exception)
            {
            }
        }

        public void SetHRMDiffCurveDisplay(int channelIndex, bool isDisplay, Color curveColor)
        {
            try
            {
                if (!isDisplay || CheckProgramMeltingDetectionSegment())
                {
                    if (mHRMDiffCurveSeries == null)
                    {
                        InitializeHRMCurveSeries(channelIndex);
                    }
                    SetCurveDisplay(mHRMDiffCurveSeries, isDisplay, curveColor);
                }
            }
            catch (Exception)
            {
            }
        }

        public void SetHRMPeakCurveDisplay(int channelIndex, bool isDisplay, Color curveColor)
        {
            try
            {
                if (!isDisplay || CheckProgramMeltingDetectionSegment())
                {
                    if (mHRMPeakCurveSeries == null)
                    {
                        InitializeHRMCurveSeries(channelIndex);
                    }
                    SetCurveDisplay(mHRMPeakCurveSeries, isDisplay, curveColor);
                }
            }
            catch (Exception)
            {
            }
        }

        public void SetMeltingDevCurveDisplay(int channelIndex, bool isDisplay, Color curveColor)
        {
            try
            {
                if (!isDisplay || CheckProgramMeltingDetectionSegment())
                {
                    if (mCellMeltingDevCurveSeries == null || mCellMeltingDevCurveSeries[channelIndex] == null)
                    {
                        InitializeMeltingCurveSeries(channelIndex);
                    }
                    SetCurveDisplay(mCellMeltingDevCurveSeries[channelIndex], isDisplay, curveColor);
                }
            }
            catch (Exception)
            {
            }
        }

        public void SetAmplificationCurveDisplay(int channelIndex, bool isDisplay, Color curveColor)
        {
            try
            {
                if (!isDisplay || CheckProgramAmplificationDetectionSegment())
                {
                    if (mCellAmplificationCurveSeries == null || mCellAmplificationCurveSeries[channelIndex] == null)
                    {
                        InitializeAmplificationCurveSeries(channelIndex);
                    }
                    SetCurveDisplay(mCellAmplificationCurveSeries[channelIndex], isDisplay, curveColor);
                }
            }
            catch (Exception)
            {
            }
        }

        public void SetRawData(int channelIndex, int cycle, double value)
        {
            int num = cycle - 1;
            try
            {
                if (num >= mLightRawData[channelIndex].Count)
                {
                    mLightRawData[channelIndex].Add(value);
                }
                else
                {
                    mLightRawData[channelIndex][num] = value;
                }
                if (mProject == null)
                {
                    return;
                }
                IProjectItem projectItem = mProject.ProjectItems[channelIndex];
                if (projectItem != null && projectItem.ChannelConfiguration.IsAvailable && projectItem.ChannelConfiguration.IsSelected)
                {
                    if (value > mRawCurveMax[channelIndex])
                    {
                        mRawCurveMax[channelIndex] = value;
                    }
                    if (value < mRawCurveMin[channelIndex])
                    {
                        mRawCurveMin[channelIndex] = value;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public void CheckRawDataList(int actualCycle, List<double> rawData)
        {
            try
            {
                double num = double.NaN;
                for (int i = 0; i < actualCycle; i++)
                {
                    if (double.IsNaN(rawData[i]))
                    {
                        if (double.IsNaN(num))
                        {
                            for (int j = i + 1; j < actualCycle; j++)
                            {
                                if (!double.IsNaN(rawData[j]))
                                {
                                    rawData[i] = rawData[j];
                                    break;
                                }
                            }
                        }
                        else
                        {
                            for (int k = i + 1; k < actualCycle; k++)
                            {
                                if (!double.IsNaN(rawData[k]))
                                {
                                    rawData[i] = num + (rawData[k] - num) / (double)(k - i + 1);
                                    break;
                                }
                            }
                        }
                    }
                    num = rawData[i];
                    if (double.IsNaN(num))
                    {
                        break;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public void CheckRawData(int channelIndex)
        {
            try
            {
                int detectCycle = mSubModule.PcrExperiment.RunningStatus.DetectCycle;
                CheckRawDataList(detectCycle, mLightRawData[channelIndex]);
            }
            catch (Exception)
            {
            }
        }

        public AnalysisError NormalizeMeltingCurve(int channelIndex)
        {
            double[] channelRawData = GetChannelRawData(channelIndex, false);
            int amplificationDetectionCount = mProject.PcrProgram.AmplificationDetectionCount;
            if (channelRawData.Length <= amplificationDetectionCount)
            {
                return AnalysisError.LessCycle;
            }
            for (int i = amplificationDetectionCount; i < channelRawData.Length; i++)
            {
                SetNormalLightData(channelIndex, i + 1, channelRawData[i]);
            }
            return AnalysisError.NA;
        }

        public void CalculateMeltingDevCurve(int channelIndex, IMeltingAnalysisParameterConfiguration meltingOption)
        {
            if (meltingOption == null)
            {
                return;
            }
            if (PcrExperiment.MainApplication == null)
            {
                PcrExperiment.MainApplication = mSubModule.PcrModule.PcrStructure.Application;
            }
            if (PcrExperiment.MainApplication == null)
            {
                return;
            }
            double[] amplificationLightDataArray = GetAmplificationLightDataArray(channelIndex);
            List<double> list = new List<double>();
            List<double> list2 = new List<double>();
            int num = 0;
            if (PcrExperiment.AmplificationStopCycle >= amplificationLightDataArray.Length - 1)
            {
                return;
            }
            double startDegree = PcrExperiment.PcrProgram.MeltingSegments[0].StartDegree;
            double stepInterval = PcrExperiment.PcrProgram.MeltingSegments[0].StepInterval;
            IMiscService miscService = (IMiscService)PcrExperiment.MainApplication.GetService("MiscService");
            for (int i = PcrExperiment.AmplificationStopCycle; i < amplificationLightDataArray.Length - 1; i++)
            {
                int num2 = ((i < PcrExperiment.AmplificationStopCycle + 2) ? i : (i - 2));
                int num3 = ((i >= amplificationLightDataArray.Length - 2) ? (amplificationLightDataArray.Length - 1) : (i + 2));
                list.Add((amplificationLightDataArray[num2] - amplificationLightDataArray[num3]) / (stepInterval * (double)(num3 - num2)));
                list2.Add(num++);
            }
            list.Add(list[list.Count - 1]);
            list2.Add(num);
            List<double>[] array = miscService.LineInterpData(list2.ToArray(), list.ToArray(), 0.1);
            List<XYDataPoint> list3 = new List<XYDataPoint>();
            if (mCellMeltingCurveSeries[channelIndex] == null || list.Count <= 1)
            {
                return;
            }
            List<double> list4 = new List<double>();
            List<double> list5 = new List<double>();
            for (int j = 0; j < array[1].Count; j++)
            {
                double num4 = array[1][j];
                double num5 = array[0][j] * stepInterval + startDegree;
                list4.Add(num4);
                list5.Add(num5);
                XYDataPoint point = new XYDataPoint(num5, num4, num5.ToString(), false);
                if (num4 > mMeltingDevCurveMax[channelIndex])
                {
                    mMeltingDevCurveMax[channelIndex] = num4;
                }
                if (mMeltingDevCurveMin[channelIndex] > num4)
                {
                    mMeltingDevCurveMin[channelIndex] = num4;
                }
                mCellMeltingDevCurveSeries[channelIndex].Points.Add(point);
            }
            XYDataPointCollection points = mCellMeltingDevCurveSeries[channelIndex].Points;
            int num6 = 0;
            int num7 = 0;
            XYDataPoint xYDataPoint = null;
            for (int k = 2; k < points.Count; k++)
            {
                if (points[k].ValueX < meltingOption.MinTemperature || points[k].ValueX > meltingOption.MaxTemperature)
                {
                    continue;
                }
                double num8 = points[k].ValueY - points[k - 1].ValueY;
                if (num8 >= 0.0)
                {
                    if (num7 == 0)
                    {
                        num6++;
                    }
                    else
                    {
                        num6 = 1;
                        num7 = 0;
                    }
                    xYDataPoint = points[k];
                    continue;
                }
                num7++;
                if (num7 >= 3 && num6 >= 3)
                {
                    if (xYDataPoint != null && xYDataPoint.ValueY >= meltingOption.NoiseThreshold)
                    {
                        list3.Add(xYDataPoint);
                    }
                    num7 = 0;
                    num6 = 0;
                }
            }
            num = 1;
            foreach (XYDataPoint item in list3)
            {
                if (num <= 10)
                {
                    SetCellInformation("MeltingTemperatureKey" + num, channelIndex, item.ValueX.ToString("0.00"));
                    num++;
                }
            }
            for (int l = num; l <= 10; l++)
            {
                SetCellInformation("MeltingTemperatureKey" + l, channelIndex, string.Empty);
            }
            SetMeltingResultDataCount(channelIndex, num - 1);
        }

        public void SetNormalLightData(int channelIndex, int cycleIndex, double value)
        {
            try
            {
                mNormalLightData[channelIndex][cycleIndex - 1] = value;
            }
            catch (Exception)
            {
            }
        }

        public void SetCurveLightData(int channelIndex, double cycle, double light)
        {
            if (mCellAmplificationCurveSeries[channelIndex] == null)
            {
                InitializeAmplificationCurveSeries(channelIndex);
            }
            XYDataPoint point = new XYDataPoint(cycle, light, cycle.ToString("0"), false);
            mCellAmplificationCurveSeries[channelIndex].Points.Add(point);
            if (light > mAmplificationCurveMax[channelIndex])
            {
                mAmplificationCurveMax[channelIndex] = light;
            }
            if (light < mAmplificationCurveMin[channelIndex])
            {
                mAmplificationCurveMin[channelIndex] = light;
            }
        }

        public void SetMeltingCurveLightData(int channelIndex, double cycle, double light)
        {
            if (mCellMeltingCurveSeries == null || mCellMeltingCurveSeries[channelIndex] == null)
            {
                InitializeMeltingCurveSeries(channelIndex);
            }
            XYDataPoint point = new XYDataPoint(cycle, light, cycle.ToString("0.00"), false);
            mCellMeltingCurveSeries[channelIndex].Points.Add(point);
            if (light > mMeltingCurveMax[channelIndex])
            {
                mMeltingCurveMax[channelIndex] = light;
            }
            if (light < mMeltingCurveMin[channelIndex])
            {
                mMeltingCurveMin[channelIndex] = light;
            }
        }

        public void SetMeltingDevCurveLightData(int channelIndex, double cycle, double light)
        {
            if (mCellMeltingDevCurveSeries[channelIndex] == null)
            {
                InitializeMeltingCurveSeries(channelIndex);
            }
            XYDataPoint point = new XYDataPoint(cycle, light, cycle.ToString("0.0"), false);
            mCellMeltingDevCurveSeries[channelIndex].Points.Add(point);
            if (light > mMeltingDevCurveMax[channelIndex])
            {
                mMeltingDevCurveMax[channelIndex] = light;
            }
            if (light < mMeltingDevCurveMin[channelIndex])
            {
                mMeltingDevCurveMin[channelIndex] = light;
            }
        }

        public void SetCycleValue(int channelIndex, double value)
        {
            if (mCycleValue == null)
            {
                InitializeAnalysisDataList();
            }
            if (mCycleValue != null)
            {
                mCycleValue[channelIndex] = value;
            }
        }

        public double GetConcentration(int channelIndex)
        {
            if (mConcentration == null)
            {
                InitializeAnalysisDataList();
            }
            if (mConcentration == null)
            {
                return 0.0;
            }
            return mConcentration[channelIndex];
        }

        public void SetConcentration(int channelIndex, double value)
        {
            if (mConcentration == null)
            {
                InitializeAnalysisDataList();
            }
            if (mConcentration != null)
            {
                mConcentration[channelIndex] = value;
            }
        }

        public double GetBaseLineAvgValue(int channelIndex)
        {
            if (mBaseLineAvgValue == null)
            {
                InitializeAnalysisDataList();
            }
            if (mBaseLineAvgValue == null)
            {
                return 0.0;
            }
            return mBaseLineAvgValue[channelIndex];
        }

        public void SetBaseLineAvgValue(int channelIndex, double value)
        {
            if (mBaseLineAvgValue == null)
            {
                InitializeAnalysisDataList();
            }
            if (mBaseLineAvgValue != null)
            {
                mBaseLineAvgValue[channelIndex] = value;
            }
        }

        public double GetNormalBaseLineAvgValue(int channelIndex)
        {
            if (mNormalBaseLineAvgValue == null)
            {
                InitializeAnalysisDataList();
            }
            if (mNormalBaseLineAvgValue == null)
            {
                return 0.0;
            }
            return mNormalBaseLineAvgValue[channelIndex];
        }

        public void SetNormalBaseLineAvgValue(int channelIndex, double value)
        {
            if (mNormalBaseLineAvgValue == null)
            {
                InitializeAnalysisDataList();
            }
            if (mNormalBaseLineAvgValue != null)
            {
                mNormalBaseLineAvgValue[channelIndex] = value;
            }
        }

        public int GetAutoBaseLineStartValue(int channelIndex)
        {
            if (mAutoBaseLineStartValue == null)
            {
                InitializeAnalysisDataList();
            }
            if (mAutoBaseLineStartValue == null)
            {
                return 0;
            }
            return mAutoBaseLineStartValue[channelIndex];
        }

        public void SetAutoBaseLineStartValue(int channelIndex, int value)
        {
            if (mAutoBaseLineStartValue == null)
            {
                InitializeAnalysisDataList();
            }
            if (mAutoBaseLineStartValue != null)
            {
                mAutoBaseLineStartValue[channelIndex] = value;
            }
        }

        public int GetAutoBaseLineStopValue(int channelIndex)
        {
            if (mAutoBaseLineStopValue == null)
            {
                InitializeAnalysisDataList();
            }
            if (mAutoBaseLineStopValue == null)
            {
                return 0;
            }
            return mAutoBaseLineStopValue[channelIndex];
        }

        public void SetAutoBaseLineStopValue(int channelIndex, int value)
        {
            if (mAutoBaseLineStopValue == null)
            {
                InitializeAnalysisDataList();
            }
            if (mAutoBaseLineStopValue != null)
            {
                mAutoBaseLineStopValue[channelIndex] = value;
            }
        }

        public int GetAutoBaseLineRotationCount(int channelIndex)
        {
            if (mAutoBaseLineRotationCount == null)
            {
                InitializeAnalysisDataList();
            }
            if (mAutoBaseLineRotationCount == null)
            {
                return 0;
            }
            return mAutoBaseLineRotationCount[channelIndex];
        }

        public void SetAutoBaseLineRotationCount(int channelIndex, int value)
        {
            if (mAutoBaseLineRotationCount == null)
            {
                InitializeAnalysisDataList();
            }
            if (mAutoBaseLineRotationCount != null)
            {
                mAutoBaseLineRotationCount[channelIndex] = value;
            }
        }

        public void SetCellInformation(string columnName, int channelIndex, string value)
        {
            try
            {
                if (PcrExperiment == null || !PcrExperiment.ColumnConfiguration.ContainsKey(columnName))
                {
                    return;
                }
                if (!mNeedReAnalysis[channelIndex])
                {
                    IColumn column = PcrExperiment.ColumnConfiguration[columnName].Tag as IColumn;
                    if (column != null && column.Classification == ColumnClassification.BasicInformation)
                    {
                        mNeedReAnalysis[channelIndex] = true;
                    }
                }
                mCellInformation[channelIndex][columnName] = value ?? string.Empty;
            }
            catch (Exception)
            {
            }
        }

        public string GetCellInformation(string columnName, int channelIndex)
        {
            try
            {
                if (mCellInformation[channelIndex].ContainsKey(columnName))
                {
                    return mCellInformation[channelIndex][columnName];
                }
                return string.Empty;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public void SetIsNeedReAnalysis(int channelIndex, bool value)
        {
            mNeedReAnalysis[channelIndex] = value;
        }

        public IPcrCell GetLastCellFromMultiTubeCells(CellDisplaySortStyle displaySortStyle)
        {
            IPcrCell pcrCell = this;
            if (mMultiTubeCells == null || mMultiTubeCells.Count == 1)
            {
                return pcrCell;
            }
            for (int i = 1; i < mMultiTubeCells.Count; i++)
            {
                int num = Convert.ToInt32(pcrCell.CellPosition.Substring(1, pcrCell.CellPosition.Length - 1));
                int num2 = Convert.ToInt32(mMultiTubeCells[i].CellPosition.Substring(1, mMultiTubeCells[i].CellPosition.Length - 1));
                string text = pcrCell.CellPosition.Substring(0, 1);
                string strB = mMultiTubeCells[i].CellPosition.Substring(0, 1);
                switch (displaySortStyle)
                {
                    case CellDisplaySortStyle.Col:
                        if (num <= num2)
                        {
                            if (num < num2)
                            {
                                pcrCell = mMultiTubeCells[i];
                            }
                            else if (text.CompareTo(strB) == -1)
                            {
                                pcrCell = mMultiTubeCells[i];
                            }
                        }
                        continue;
                    case CellDisplaySortStyle.Block:
                        {
                            string moduleID = pcrCell.SubModule.PcrModule.ModuleID;
                            string moduleID2 = mMultiTubeCells[i].SubModule.PcrModule.ModuleID;
                            if (moduleID.CompareTo(moduleID2) == -1)
                            {
                                pcrCell = mMultiTubeCells[i];
                            }
                            break;
                        }
                }
                switch (text.CompareTo(strB))
                {
                    case -1:
                        pcrCell = mMultiTubeCells[i];
                        continue;
                    case 1:
                        continue;
                }
                if (num < num2)
                {
                    pcrCell = mMultiTubeCells[i];
                }
            }
            return pcrCell;
        }

        protected double CalculateBaseLineAverage(double[] channelData, int startBaseLine, int stopBaseLine)
        {
            try
            {
                double num = 0.0;
                for (int i = startBaseLine; i <= stopBaseLine; i++)
                {
                    num += ((!double.IsNaN(channelData[i - 1])) ? channelData[i - 1] : 0.0);
                }
                return num / (double)(stopBaseLine - startBaseLine + 1);
            }
            catch (Exception)
            {
                for (int j = 0; j < channelData.Length; j++)
                {
                    if (!double.IsNaN(channelData[j]))
                    {
                        return channelData[j];
                    }
                }
                return 1.0;
            }
        }

        public void CaculateLightCrossTalk(IPcrExperimentProject project)
        {
            if (!SaveLightRawData())
            {
                return;
            }
            List<double> list = new List<double>();
            IProjectItem[] projectItems = project.ProjectItems;
            foreach (IProjectItem projectItem in projectItems)
            {
                if (projectItem == null)
                {
                    continue;
                }
                if (projectItem.ChannelConfiguration.IsAvailable && projectItem.ChannelConfiguration.IsSelected)
                {
                    IAbsoluteAnalysisParameterConfiguration absoluteAnalysisParameterConfiguration = projectItem.AnalysisParameterConfiguration as IAbsoluteAnalysisParameterConfiguration;
                    double[] array = mTempLightRawData[projectItem.ChannelConfiguration.Position].ToArray();
                    int num = absoluteAnalysisParameterConfiguration.StartBaseline;
                    int num2 = absoluteAnalysisParameterConfiguration.StopBaseline;
                    if (num >= array.Length)
                    {
                        num = array.Length - 2;
                        num2 = array.Length - 1;
                    }
                    else if (num2 >= array.Length)
                    {
                        num2 = array.Length - 1;
                    }
                    double num3 = 1.0;
                    try
                    {
                        num3 = CalculateBaseLineAverage(array, num, num2);
                    }
                    catch (Exception)
                    {
                        num3 = 1.0;
                    }
                    list.Add(num3);
                }
                else
                {
                    list.Add(1.0);
                }
            }
            if (project.CrossTalkValue == null)
            {
                project.CrossTalkValue = new double[project.ProjectItems.Length, project.ProjectItems.Length];
            }
            IProjectItem[] projectItems2 = project.ProjectItems;
            foreach (IProjectItem projectItem2 in projectItems2)
            {
                if (projectItem2 == null || !projectItem2.ChannelConfiguration.IsAvailable || !projectItem2.ChannelConfiguration.IsSelected)
                {
                    continue;
                }
                int position = projectItem2.ChannelConfiguration.Position;
                for (int k = 0; k < mTempLightRawData[position].Count; k++)
                {
                    double num4 = 0.0;
                    IProjectItem[] projectItems3 = project.ProjectItems;
                    foreach (IProjectItem projectItem3 in projectItems3)
                    {
                        if (projectItem3 != null)
                        {
                            int position2 = projectItem3.ChannelConfiguration.Position;
                            if (projectItem3.ChannelConfiguration.Position != position && projectItem3.ChannelConfiguration.IsAvailable && projectItem3.ChannelConfiguration.IsSelected && mTempLightRawData[position2] != null)
                            {
                                num4 += mTempLightRawData[position2][k] * (project.CrossTalkValue[position2, position] / 100.0);
                            }
                        }
                    }
                    mLightRawData[position][k] = mLightRawData[position][k] - num4;
                }
            }
        }

        public void CurveAddPoint(int channelIndex, double light, double cycle, bool empty)
        {
            if (mSubModule == null || mSubModule.PcrExperiment == null)
            {
                return;
            }
            IPcrProgram pcrProgram = mSubModule.PcrExperiment.PcrProgram;
            if (pcrProgram == null)
            {
                return;
            }
            if (cycle <= (double)pcrProgram.AmplificationDetectionCount)
            {
                RawCurveAddPoint(channelIndex, light, (int)cycle, empty);
            }
            else if (pcrProgram.MeltingDetectionCount > 0)
            {
                if (pcrProgram.MeltingSegments != null && pcrProgram.MeltingSegments[0].IsAvailable)
                {
                    double startDegree = pcrProgram.MeltingSegments[0].StartDegree;
                    startDegree += (cycle - (double)pcrProgram.AmplificationDetectionCount - 1.0) * pcrProgram.MeltingSegments[0].StepInterval;
                    MeltingCurveAddPoint(channelIndex, light, startDegree, empty);
                }
                else if (pcrProgram.HRMSegments != null && pcrProgram.HRMSegments[0].IsAvailable)
                {
                    double startDegree2 = pcrProgram.HRMSegments[0].StartDegree;
                    startDegree2 += (cycle - (double)pcrProgram.AmplificationDetectionCount - 1.0) * pcrProgram.HRMSegments[0].StepInterval;
                    MeltingCurveAddPoint(channelIndex, light, startDegree2, empty);
                }
            }
        }

        public void RawCurveAddPoint(int channelIndex, double light, int cycle, bool empty)
        {
            mCellRawCurveSeries[channelIndex].Points.Add(new XYDataPoint(cycle, light, "", empty));
        }

        public void MeltingCurveAddPoint(int channelIndex, double light, double degree, bool empty)
        {
            mCellMeltingCurveSeries[channelIndex].Points.Add(new XYDataPoint(degree, light, "", empty));
        }

        public void AddPointToRawCurve(int channelIndex, bool isPointLimit, int limitValue)
        {
            try
            {
                if (mCellRawCurveSeries == null)
                {
                    for (int i = 0; i < mLightRawData.Length; i++)
                    {
                        InitializeRawCurveSeries(i);
                    }
                }
                if (!PcrExperiment.IsChannelUsed[channelIndex])
                {
                    return;
                }
                if (mCellRawCurveSeries[channelIndex] == null)
                {
                    InitializeRawCurveSeries(channelIndex);
                }
                mCellRawCurveSeries[channelIndex].Points.Clear();
                int num = mLightRawData[channelIndex].Count;
                if (mProject.PcrProgram.PcrExperimentType != 0 && num > mProject.PcrProgram.AmplificationDetectionCount)
                {
                    num = mProject.PcrProgram.AmplificationDetectionCount;
                }
                if (isPointLimit && num > limitValue)
                {
                    num = limitValue;
                }
                for (int j = 0; j < num; j++)
                {
                    if (!double.IsNaN(mLightRawData[channelIndex][j]))
                    {
                        RawCurveAddPoint(channelIndex, mLightRawData[channelIndex][j], j + 1, false);
                        if (mLightRawData[channelIndex][j] > mRawCurveMax[channelIndex])
                        {
                            mRawCurveMax[channelIndex] = mLightRawData[channelIndex][j];
                        }
                        if (mLightRawData[channelIndex][j] < mRawCurveMin[channelIndex])
                        {
                            mRawCurveMin[channelIndex] = mLightRawData[channelIndex][j];
                        }
                    }
                }
                if (isPointLimit)
                {
                    for (int k = num; k < mLightRawData[channelIndex].Count; k++)
                    {
                        mLightRawData[channelIndex][k] = double.NaN;
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public void AddPointToMeltingRawCurve(int channelIndex, int startCycle, int stopCycle)
        {
            if (mCellMeltingCurveSeries == null)
            {
                for (int i = 0; i < mLightRawData.Length; i++)
                {
                    InitializeMeltingCurveSeries(i);
                }
            }
            if (!PcrExperiment.IsChannelUsed[channelIndex])
            {
                return;
            }
            if (mCellMeltingCurveSeries[channelIndex] == null)
            {
                InitializeMeltingCurveSeries(channelIndex);
            }
            mCellMeltingCurveSeries[channelIndex].Points.Clear();
            IPcrProgram pcrProgram;
            if (mProject == null || mProject.PcrProgram == null)
            {
                if (mSubModule.PcrExperiment == null || mSubModule.PcrExperiment.PcrProgram == null)
                {
                    return;
                }
                pcrProgram = mSubModule.PcrExperiment.PcrProgram;
            }
            else
            {
                pcrProgram = mProject.PcrProgram;
            }
            double startDegree = pcrProgram.MeltingSegments[0].StartDegree;
            double stepInterval = pcrProgram.MeltingSegments[0].StepInterval;
            if (startCycle < 0)
            {
                startCycle = 0;
            }
            if (stopCycle > mLightRawData[channelIndex].Count)
            {
                stopCycle = mLightRawData[channelIndex].Count;
            }
            for (int j = startCycle; j < stopCycle; j++)
            {
                if (!double.IsNaN(mLightRawData[channelIndex][j]))
                {
                    double degree = startDegree + (double)(j - startCycle) * stepInterval;
                    MeltingCurveAddPoint(channelIndex, mLightRawData[channelIndex][j], degree, false);
                    if (mLightRawData[channelIndex][j] > mMeltingCurveMax[channelIndex])
                    {
                        mMeltingCurveMax[channelIndex] = mLightRawData[channelIndex][j];
                    }
                    if (mLightRawData[channelIndex][j] < mMeltingCurveMin[channelIndex])
                    {
                        mMeltingCurveMin[channelIndex] = mLightRawData[channelIndex][j];
                    }
                }
            }
        }

        public void AddLightDataCycle(int addedCycleCount, int dataByteCount)
        {
            if (mLightRawByteData == null)
            {
                mLightRawByteData = new List<byte[,]>();
            }
            if (mLightRawData == null)
            {
                mLightRawData = new List<double>[mChannelCount];
                for (int i = 0; i < mChannelCount; i++)
                {
                    mLightRawData[i] = new List<double>();
                }
            }
            for (int j = 0; j < addedCycleCount; j++)
            {
                mLightRawByteData.Add(IntializeNewDataArray(dataByteCount));
                for (int k = 0; k < mChannelCount; k++)
                {
                    mLightRawData[k].Add(0.0);
                }
            }
        }

        public void RemoveLightDataCycle(int removedCycleCount)
        {
            int index = mLightRawByteData.Count - removedCycleCount;
            mLightRawByteData.RemoveRange(index, removedCycleCount);
            for (int i = 0; i < mChannelCount; i++)
            {
                mLightRawData[i].RemoveRange(index, removedCycleCount);
            }
        }

        public void AddNewLightData(int cycle, int channelIndex, byte[] data)
        {
            try
            {
                int num = cycle - 1;
                if (num >= mLightRawByteData.Count)
                {
                    mLightRawByteData.Add(IntializeNewDataArray(data.Length));
                }
                for (int i = 0; i < data.Length; i++)
                {
                    mLightRawByteData[num][channelIndex, i] = data[i];
                }
            }
            catch (Exception)
            {
            }
        }

        public void ExportRawByteData(int channelIndex)
        {
            if (mLightRawByteData == null)
            {
                return;
            }
            StreamWriter streamWriter = null;
            try
            {
                streamWriter = new StreamWriter("c:\\1.txt", true);
                streamWriter.WriteLine("Cell " + mCellPosition);
                streamWriter.WriteLine("Channel " + (channelIndex + 1));
                for (int i = 0; i < mLightRawByteData.Count; i++)
                {
                    int length = mLightRawByteData[i].GetLength(1);
                    streamWriter.WriteLine("Cycle " + (i + 1));
                    for (int j = 0; j < length; j++)
                    {
                        streamWriter.WriteLine(mLightRawByteData[i][channelIndex, j]);
                    }
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Close();
                }
            }
        }

        public bool SaveLightRawData()
        {
            if (!mIsSaveTempLightRawData)
            {
                if (mLightRawData == null)
                {
                    return false;
                }
                mTempLightRawData = new List<double>[mLightRawData.Length];
                for (int i = 0; i < mLightRawData.Length; i++)
                {
                    mTempLightRawData[i] = new List<double>();
                    for (int j = 0; j < mLightRawData[i].Count; j++)
                    {
                        mTempLightRawData[i].Add(mLightRawData[i][j]);
                    }
                }
                mIsSaveTempLightRawData = true;
            }
            return true;
        }

        public void LoadLightRawData()
        {
            if (!mIsSaveTempLightRawData || mTempLightRawData == null)
            {
                return;
            }
            mLightRawData = new List<double>[mTempLightRawData.Length];
            for (int i = 0; i < mTempLightRawData.Length; i++)
            {
                mLightRawData[i] = new List<double>();
                for (int j = 0; j < mTempLightRawData[i].Count; j++)
                {
                    mLightRawData[i].Add(mTempLightRawData[i][j]);
                }
            }
            mIsSaveTempLightRawData = false;
        }

        public void SetMeltingResultDataCount(int channelIndex, int value)
        {
            if (mMeltingResultDataCount == null)
            {
                InitializeMeltingResultDataCount();
            }
            if (mMeltingResultDataCount != null && channelIndex < mMeltingResultDataCount.Length)
            {
                mMeltingResultDataCount[channelIndex] = value;
            }
        }

        public int GetMeltingResultDataCountFromCurve(int channelIndex)
        {
            XYSeries meltingCurveSeries = GetMeltingCurveSeries(channelIndex);
            if (meltingCurveSeries == null)
            {
                return 0;
            }
            return meltingCurveSeries.Points.Count;
        }

        public int GetMeltingResultDataCount(int channelIndex)
        {
            if (mMeltingResultDataCount == null)
            {
                InitializeMeltingResultDataCount();
            }
            if (mMeltingResultDataCount == null)
            {
                return 0;
            }
            if (channelIndex >= mMeltingResultDataCount.Length)
            {
                return 0;
            }
            return mMeltingResultDataCount[channelIndex];
        }

        public void SetRuleCalculationValueCount(int channelIndex, int value)
        {
            if (mRuleCalculationValueCount == null)
            {
                InitializeRuleCalculationValueCount();
            }
            if (mRuleCalculationValueCount != null && channelIndex < mRuleCalculationValueCount.Length)
            {
                mRuleCalculationValueCount[channelIndex] = value;
            }
        }

        public int GetRuleCalculationDataCount(int channelIndex)
        {
            if (mRuleCalculationValueCount == null)
            {
                InitializeRuleCalculationValueCount();
            }
            if (mRuleCalculationValueCount == null)
            {
                return 0;
            }
            if (channelIndex >= mRuleCalculationValueCount.Length)
            {
                return 0;
            }
            return mRuleCalculationValueCount[channelIndex];
        }
    }
}
